import Vue from 'vue'
import Vuex from 'vuex'

import {
  loadUser,
  setUser,
  deleteUser
} from '@/common/js/cache'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    collapse: false,
    user: loadUser()
  },
  getters: {
    collapse: state => state.collapse,
    user: state => JSON.parse(state.user)
  },
  mutations: {
    TOGGLE_COLLAPSE: state => {
      state.collapse = !state.collapse
    },
    SAVE_USER: (state, user) => {
      state.user = user
    },
    DEL_USER: (state, user) => {
      state.user = user
    }
  },
  actions: {
    toggleCollapse: ({
      commit
    }) => {
      commit('TOGGLE_COLLAPSE')
    },
    saveUser: ({
      commit
    }, user) => {
      commit('SAVE_USER', setUser(user))
    },
    delUser: ({
      commit
    }, user) => {
      commit('DEL_USER', deleteUser(user))
    }
  }
})